﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BLL;
using WeChat.Common;

namespace WeChat.Manager
{
    public partial class index : System.Web.UI.Page
    {
        MediaBLL mb = new MediaBLL();
        GetMedia gm = new GetMedia();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!ForLogin.IsLogin())
            {
                Response.Redirect("../Login.aspx"); 
                return;
            }
        }
        //同步素材
        protected void Synchronous(object sender, EventArgs e)
        {
            Sync.Enabled = false;
            //先清空表再插入
            mb.truncateMedia("media");
            int i = gm.getMedia("news", 0, 20);
            updates("news",i);
            int m = gm.getMedia("image", 0, 20);
            updates("image", m);
            int k = gm.getMedia("voice", 0, 20);
            updates("voice", k);
            int o = gm.getMedia("video", 0, 20);
            updates("video", o);
            
            Page.ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>tips();</script>");
            Sync.Enabled = true;
        }

        //同步关注用户
        protected void SynchronousUser(object sender, EventArgs e)
        {
            mb.truncateMedia("subscribes");
            new GetUsers().synchroUsers();
            Page.ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>tips();</script>");
        }
        /// <summary>
        /// 一次最多获取20个同类型素材，获取全部素材
        /// </summary>
        /// <param name="type">素材类型</param>
        /// <param name="i">素材数量</param>
        private void updates(string type,int i)
        {
            if (i > 20)
            {
                for (int m = 1; m <= i / 20; m++)
                {
                    gm.getMedia(type, 20*m, 20);
                }
            }
        }

    }
}
